﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace Cartoons
{
    public partial class SearchResultViewer : UserControl
    {
        public static readonly DependencyProperty IndexesProperty =
            DependencyProperty.Register("Indexes", typeof(short[]), typeof(SearchResultViewer), new PropertyMetadata(null, IndexesChangedCallback));

        private static void IndexesChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var search = sender as SearchResultViewer;
            search.PlaceElements();
        }

        public short[] Indexes
        {
            get { return (short[])GetValue(IndexesProperty); }
            set { SetValue(IndexesProperty, value); }
        }



        public short? SelectedIndex
        {
            get { return (short?)GetValue(SelectedIndexProperty); }
            set { SetValue(SelectedIndexProperty, value); }
        }

        public static readonly DependencyProperty SelectedIndexProperty =
            DependencyProperty.Register("SelectedIndex", typeof(short?), typeof(SearchResultViewer), new PropertyMetadata(null, OnSelectedIndexChanged));

        private static void OnSelectedIndexChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var target = sender as SearchResultViewer;

            if (e.OldValue == null && e.NewValue == null)
                return;

            if (e.NewValue == null)
            {
                if (Convert.ToInt16(e.OldValue) < target.image.SubImages.Count)
                    target.ClearSelection(Convert.ToInt16(e.OldValue));
            }
            else
                if (Convert.ToInt16(e.NewValue) < target.image.SubImages.Count)
                    target.SelectImage(Convert.ToInt16(e.NewValue));
        }

        public TimeSpan Duration
        {
            get { return (TimeSpan)GetValue(DurationProperty); }
            set { SetValue(DurationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Duration.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DurationProperty =
            DependencyProperty.Register("Duration", typeof(TimeSpan), typeof(SearchResultViewer), new PropertyMetadata(new TimeSpan(0, 0, 2)));


    }
}
